<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="渠道商" prop="companyId">
        <el-select v-model="queryParams.companyId" filterable clearable placeholder="请选择渠道商">
          <el-option
            v-for="item in company_data"
            :key="item.id"
            :label="item.company"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workerJob:company_price:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate()"
          v-hasPermi="['workerJob:company_price:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workerJob:company_price:remove']"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                :loading="exportLoading"
                @click="handleExport"
                v-hasPermi="['workerJob:company_price:export']"
              >导出</el-button>
            </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="company_priceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="渠道商" align="center" prop="company"/>
      <el-table-column label="价格生效时间" align="center" prop="priceBeginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.priceBeginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格结束时间" align="center" prop="priceEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.priceEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="白班价格" align="center" prop="dayPrice"/>
      <el-table-column label="夜班价格" align="center" prop="nightPrice"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workerJob:company_price:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workerJob:company_price:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改渠道商时段价格记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position='left'>
        <el-form-item label="渠道商" prop="companyId">
          <el-select v-model="form.companyId" filterable  clearable placeholder="请选择渠道商">
            <el-option
              v-for="item in company_data"
              :key="item.id"
              :label="item.company"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生效-结束时间" prop="daterange">

          <el-date-picker
            v-model="form.daterange"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="生效时间"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="白班价格" prop="dayPrice">
          <el-input type="number" v-model="form.dayPrice" placeholder="请输入白班价格"/>
        </el-form-item>
        <el-form-item label="夜班价格" prop="nightPrice">
          <el-input type="number"  v-model="form.nightPrice" placeholder="请输入夜班价格"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="submit" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompany_price,
  delCompany_price,
  addCompany_price,
  updateCompany_price,
  listCompany
} from "@/api/job/company/companyPriceApi";

export default {
  name: "company_price",
  data() {
    return {
      // 渠道商数据
      company_data: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      //勾选对象
      single_data: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道商时段价格记录表格数据
      company_priceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        priceBeginTime: null,
        priceEndTime: null,
        dayPrice: null,
        nightPrice: null,
      },
      // 表单参数
      form: {},
      //提交表单
      submit:false,
      // 表单校验
      rules: {
        companyId: [
          {required: true, message: "渠道商不能为空", trigger: "blur"}
        ],
        daterange: [
          {required: true, message: "价格生效-结束时间不能为空", trigger: "blur"}
        ],
        dayPrice: [
          {required: true, message: "白班价格不能为空", trigger: "blur"}
        ],
        nightPrice: [
          {required: true, message: "夜班价格不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.handleCompanyOptions();
  },
  methods: {
    /** 查询渠道商时段价格记录列表 */
    getList() {
      this.loading = true;
      listCompany_price(this.queryParams).then(response => {
        this.company_priceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cpId: null,
        companyId: null,
        priceBeginTime: null,
        priceEndTime: null,
        dayPrice: null,
        nightPrice: null,
        daterange: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cpId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.single_data = selection[selection.length-1]
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加渠道商时段价格记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      row = row||this.single_data
      console.log("修改按钮操作", row);
      this.form = row;
      this.$set(this.form,'daterange',[row.priceBeginTime,row.priceEndTime]);
      this.open = true;
      this.title = "修改渠道商时段价格记录";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //debugger
          this.form.priceBeginTime = this.form.daterange[0]
          this.form.priceEndTime = this.form.daterange[1]
          this.submit = true;
          if (this.form.cpId != null) {
            updateCompany_price(this.form).then(response => {
              debugger
              if (response.code === 200){
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCompany_price(this.form).then(response => {
              if (response.code === 200){
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
          this.submit = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.cpId || this.ids;
      this.$confirm('是否确认删除选中的数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delCompany_price(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //获取启用的渠道商
    handleCompanyOptions() {
      listCompany({status: 1}).then(response => {
        this.company_data = response.data
      });
    }
  }
};
</script>

<style scoped>
.el-select {
  width: 100%;
}
.el-date-editor{
  width: auto;
}
</style>
